import request from './request';

// 获取订单列表
export function getOrderList(params) {
  return request({
    url: '/api/omm/orders/',
    method: 'get',
    params
  });
}

// 获取提成明细列表
export function getCommissionDetails(params) {
  return request({
    url: '/api/omm/orders/commission/',
    method: 'get',
    params
  });
}

// 导出提成明细
export function exportCommissionDetails(params) {
  return request({
    url: '/api/omm/orders/commission/export/',
    method: 'get',
    params,
    responseType: 'blob',
    timeout: 60000 // 60秒超时
  });
}

// 获取单个订单
export function getOrder(id) {
  return request({
    url: `/api/omm/orders/${id}/`,
    method: 'get'
  });
}

// 新增订单
export function createOrder(data) {
  return request({
    url: '/api/omm/orders/',
    method: 'post',
    data
  });
}

// 更新订单
export function updateOrder(id, data) {
  return request({
    url: `/api/omm/orders/${id}/`,
    method: 'put',
    data
  });
}

// 删除订单
export function deleteOrder(id) {
  return request({
    url: `/api/omm/orders/${id}/`,
    method: 'delete'
  });
}

// 批量删除订单
export function batchDeleteOrders(ids) {
  return request({
    url: '/api/omm/orders/batch_delete/',
    method: 'post',
    data: { ids }
  });
}

// 批量标记开票
export function batchMarkInvoice(ids) {
  return request({
    url: '/api/omm/orders/batch_mark_invoice/',
    method: 'post',
    data: { ids }
  });
}

// 导出订单数据
export function exportOrders(params) {
  console.log('调用导出API，参数:', params);
  return request({
    url: '/api/omm/orders/export/',
    method: 'get',
    params,
    responseType: 'blob',
    timeout: 60000, // 60秒超时，给大文件导出足够时间
    // 导出进度处理
    onDownloadProgress: (progressEvent) => {
      if (progressEvent.lengthComputable) {
        const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
        console.log(`下载进度: ${percentCompleted}%`);
      }
    }
  });
}

// 导入订单数据
export function importOrders(file) {
  console.log('importOrders API被调用，文件:', file.name);

  // 创建FormData对象
  const formData = new FormData();
  formData.append('file', file);
  console.log('FormData已创建并添加文件');

  // 返回请求
  return request({
    url: '/api/omm/orders/import/',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    // 添加上传进度处理
    onUploadProgress: (progressEvent) => {
      const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
      console.log(`上传进度: ${percentCompleted}%`);
    },
    // 请求超时设置
    timeout: 60000, // 60秒超时，给大文件足够上传时间
    transformResponse: [function (data) {
      try {
        // 尝试解析JSON
        const jsonData = JSON.parse(data);
        // 确保响应有正确的格式
        if (jsonData && !jsonData.code) {
          // 如果后端没有返回code字段，手动添加
          return {
            code: 200,
            message: jsonData.message || '导入成功',
            data: jsonData
          };
        }
        return jsonData;
      } catch (e) {
        // 如果解析失败，返回原始数据
        console.error('解析响应数据失败:', e);
        return data;
      }
    }]
  });
}

// 获取订单导入模板
export function getImportTemplate() {
  return request({
    url: '/api/omm/orders/import_template/',
    method: 'get',
    responseType: 'blob'
  });
} 